package org.example.thread;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class CompletableFutureCompose {
    /**
     * 子任务和父任务公用同一个线程
     * @throws ExecutionException
     * @throws InterruptedException
     */
    public static void thenRunAsync() throws ExecutionException, InterruptedException {
        CompletableFuture<Integer> cf1= CompletableFuture.supplyAsync(()->{
            System.out.println(Thread.currentThread()+"cf1 do..");
            return 1;
        });
        CompletableFuture<Void> cf2= cf1.thenRunAsync(()->{
            System.out.println(Thread.currentThread()+"cf2 do..");
        });
        System.out.println("cf1结果"+cf1.get());
        System.out.println("cf2结果"+cf2.get());

    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        thenRunAsync();
    }
}
